package com.example.loginme;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity2 extends AppCompatActivity {
    private CheckBox mCheckBox;
    private Button mRegisterButton;//注册按钮
    private Button mLoginButton;//登录按钮

    private EditText mUser;
    private EditText mKey;

    public static String Getuserpassword() {
    }

    public static String Getusername() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        mUser=(EditText)findViewById(R.id.input_user_text);
        mKey=(EditText)findViewById(R.id.input_key_text);
        mRegisterButton=(Button)findViewById(R.id.register_button);
        mLoginButton=(Button)findViewById(R.id.login_button);
        mCheckBox=(CheckBox)findViewById(R.id.checkbox_text);
        //检查是否有账号密码，如果有，需要回显
        Map<String, String> map=SavePath.Get(this);
        if(map!=null){
            mUser.setText(map.get("user"));
            mKey.setText(map.get("key"));
        }



        mLoginButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v)  //给登录按钮设置监听器
            {
                String username = mUser.getText().toString();
                String userpassword = mKey.getText().toString();
                String username1=MainActivity2.Getusername();//获取注册值
                String userpassword1=MainActivity2.Getuserpassword();//获取注册值

                //if(!username.equals(username1))//验证
                //{Toast.makeText(MainActivity.this, username1,
                // Toast.LENGTH_SHORT).show();}


                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(userpassword)){
                    Toast.makeText(MainActivity2.this, R.string.message1,
                            Toast.LENGTH_SHORT).show();
                }
                if(mCheckBox.isChecked()){
                    boolean result=SavePath.Save(MainActivity2.this,username,userpassword);
                    if(result){
                        Toast.makeText(MainActivity2.this, R.string.message2,
                                Toast.LENGTH_SHORT).show();
                    }
                }

                if(username.equals(username1)&&userpassword.equals(userpassword1)) {
                    Toast.makeText(MainActivity2.this, R.string.press_login_button1,
                            Toast.LENGTH_SHORT).show();//事件触发,显示欢迎信息
                    //下面跳转问卷界面
                }
                else {
                    Toast.makeText(MainActivity2.this,R.string.press_login_button2,
                            Toast.LENGTH_SHORT).show();//事件触发,显示登录失败
                }

            }
        });

        mRegisterButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                intent.setClass(MainActivity2.this, MainActivity.class);
                startActivity(intent);//跳转注册界面

                finish();
            }
        });

    }
}

